#!/bin/bash

source /home/shell/util.sh

if [ -f "/usr/bin/docker" ];
then
  echo "docker已安装："`docker -v`
  exit;
fi

# 检查防火墙状态
firewall-must-run

# 删除系统自带docker
apt-get --purge remove docker docker.io containerd runc

# 使用阿里云镜像
mkdir -p /etc/docker
tee /etc/docker/daemon.json<<EOF
{
   "registry-mirrors" : ["https://nwgq096a.mirror.aliyuncs.com","https://0bc1c2bdb880f3630f6fc015c1f33900.mirror.swr.myhuaweicloud.com"],
   "insecure-registries": []
}
EOF

apt-get -y update
apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common

# 添加密钥
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# curl -fsSL https://repo.huaweicloud.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - 
# 新增docker安装源
add-apt-repository -y "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# add-apt-repository "deb [arch=amd64] https://repo.huaweicloud.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" 
apt-get -y update

echo -e "=============选择安装版本============="
apt-cache madison docker-ce
echo -e "=============选择安装版本============="
echo -e "请输入安装版本[回车默认最新版本]：\c"
read docker_version
docker_install="docker-ce"
if [ -n "$docker_version" ];
then
  docker_install=$docker_install"="$docker_version
fi


apt -y install $docker_install


# 设置docker驱动Cgroup=>systemd
sed -i "s@ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock@ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock --exec-opt native.cgroupdriver=systemd@g" /usr/lib/systemd/system/docker.service
# 关闭swap
swapoff  -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
# 启动docker服务
# systemctl enable --now docker
systemctl daemon-reload
systemctl restart docker

#firewall-cmd --permanent --zone=trusted --add-interface=docker0
firewall-cmd --permanent --zone=public --add-port=2375/tcp
firewall-cmd --reload

# 显示docker信息
echo "============docker============"
docker info
dockerv=`docker -v`
echo "客户端版本："$dockerv
docker_serv_ver=`docker info|grep "Server Version"|awk '{print $3}'`
echo "服务端版本："$docker_serv_ver
echo "============docker============"